/*
 * This is a manager for the client collection. 
 * All clients that are connected to the provider will be listed in the variable 'clients'.
 * 
 * The manager is design with the singleton pattern to ensure that only one instance can be created of it.
 */

package telco.provider.manager;

import java.util.Vector;

import swa2012.telco.broker.beans.Client;

public class ClientManager {
	
	private static final ClientManager instance = new ClientManager();
	private Vector<Client> clients = new Vector<Client>(); 
	
	//Constructor
    private ClientManager() 
    {
    	
    }
 
    //Gets the singleton instance of this class
    public static ClientManager getInstance() 
    {
        return instance;
    }
	
    //Returns all registered Clients
	public Vector<Client> getClients()
	{
		return clients;
	}
	
	//Returns all registered Clients
	public Vector<Client> getAvailableClients()
	{
		try
		{
			return new Vector<Client>(BrokerManager.getInstance().getClients());
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return clients;
	}
	
	public boolean isClientConnected(Client c)
	{
		return clients.contains(c);
	}
	
	//Adds client and returns true if adding worked
	public boolean addClient(Client c)
	{
		if(!clients.contains(c))
		{
			return clients.add(c);
		}
		else return false;
	}
	
	//Removes client if it exists in the collection and, in this case, returns true. Otherwise returns false.
	public boolean removeClient(Client c)
	{
		/*
		 * Extended Clients.java with 'Equals' method
		 */		
		return clients.remove(c);
	}
	
	//Removes all registered clients for cleanup
	public void deregisterAllClients()
	{
		clients.clear();
	}
}
